const path = require('path');
const GenerateRoutesAndFiles = require('./create');
const router = new GenerateRoutesAndFiles();
module.exports = (api, opts) => {
  // 实时监控
  if (process.env.NODE_ENV === 'development') {
    const chokidar = require('chokidar');
    const watcher = chokidar.watch(path.resolve(process.cwd(), './src/pages'), {
      ignored: /[\/\\]\./, persistent: true
    });
    watcher
      .on('add', function(path) {
        router.createRouter();
      })
      .on('addDir', function(path) {
        router.createRouter();
      })
      .on('unlink', function(path) {
        router.createRouter();
      })
      .on('unlinkDir', function(path) {
        router.createRouter();
      });
  }

  // 命令的方式
  api.registerCommand(
    'router',
    {
      description: '',
      usage: 'vue-cli-service router'
    },
    () => {
      router.createRouter();
    });

  // run方式
  api.configureWebpack(webpackconfig => {
    // eslint-disable-next-line no-new
    router.createRouter();
  });
};
